import { ComponentType } from '@/plugins/form/form-render/constants/component-types';
import { ICondition } from '../../../types';
import { parseValueFromOirginValue } from '@/plugins/form/form-render/utils/detail-parser';

// const handleSimpleCondition = (condition: ICondition, value: any) => {
//   if (condition.conditionName && condition.conditionValue === value) {
//     return true;
//   }
//   return false;
// };

// 多选，判断的时候，如果所有的值都选上了则认为是成立的 value必须包含conditionValue里面的选项
const handlerCheckboxSelectCondition = (condition: ICondition, value: any) => {
  const conditionValue = condition.conditionValue;
  if (!conditionValue) {
    return true;
  }
  if (!value) {
    return false;
  }
  value = parseValueFromOirginValue(value);
  value = value ? value.split(';') : [];
  console.log('value', value);
  // 对于所有条件中的数据，值中都有设置到
  for (let i = 0; i < conditionValue.length; i++) {
    if (value.indexOf(conditionValue[i]) === -1) {
      return false;
    }
  }
  return true;
};

const handlerRadioSelectCondition = (condition: ICondition, value: any) => {
  const conditionValue = condition.conditionValue;
  if (typeof conditionValue === 'function') {
    return conditionValue(value);
  }
  return conditionValue;
};
const handlers: { [index: string]: any } = {
  // [ComponentType.RadioSelect]: handleSimpleCondition,
  [ComponentType.CheckboxSelect]: handlerCheckboxSelectCondition,
  [ComponentType.RadioSelect]: handlerRadioSelectCondition
};

export const defaultHandler = (el: ICondition, value: any) => {
  // if (!el.conditionValue) {
  //   return true;
  // }
  if (el.conditionValue === parseValueFromOirginValue(value)) {
    return true;
  }
  return false;
};

export default handlers;
